<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>一元二次方程</title>
    <script type="text/javascript">
        window.onload = function () {
            var equation = document.getElementById("equation").value
            alert(equation)
            var show = document.getElementById("show");
            var x;
            var a;
            var b;
            var c;
            var x1;
            var x2;
            var array = ["+", "=", "-", "/"];
            document.getElementById("calculate").onclick = function () {
                for (let i = 0; i < equation.length; i++) {
                    //获取a
                    if (equation[i] == "^") {
                        x = equation[i - 1];
                        if (i <= 1) {
                            a = 1;
                            alert("a=" + a)
                        } else {
                            a = equation[i - 2];
                            alert("a=" + a)
                        }
                    }
                }
                for (let j = 0; j < equation.length; j++) {
                    //获取c
                    if (equation[j] == "+" || equation[j] == "-") {
                        if (equation[j + 2] != x) {
                            if (equation[j] == "-") {
                                c = -equation[j + 1];
                                alert("c=" + c)
                                break
                            }
                            c = equation[j + 1];
                            alert("c=" + c)
                            break
                        }
                    } else if (equation[j] == "=") {
                        c = equation[j + 1];
                        if (c > 0) {
                            c = -c;
                        } else {
                            c = 0;
                        }
                        alert("c=" + c)
                    }
                    //获取b
                    if (equation[j] == x) {
                        if (j <= 0 && equation[j + 1] != "^") {
                            b = 1;
                        } else if (j >= 1) {
                            b = equation[j - 1];
                            alert("b=" + b)
                        }
                    }
                }
                //判断方程有无根
                var root = b * b - 4 * a * c;
                alert(root);
                if (root < 0) {
                    show.innerText = "根无解"
                } else if (root == 0) {
                    x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;
                    show.innerText = "根有一个解:" + x1
                } else {
                    x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;
                    x2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a;
                    show.innerText = "根有两个解:\n" + "x1:" + x1 + "\n" + "x2:" + x2
                }
            }
        }
    </script>
</head>
<body>
请输入要计算的一元二次方程:<input id="equation" type="text"><br>
<input id="calculate" style="background-color: aqua;width: 80px;height: 50px" type="button" value="求根"><br>
<span id="show" style="color: brown;font-size: 20px"></span>
</body>
</html>